// pages/resume-pages/edit-info/edit-info.js
let _preEnterpriseLogo = '';
const app = getApp();
import Toast from '../../../component/toast/toast';
const uploadFile = require('../../../lib/aliyun/uploadAliyun.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        profileUrl:'',
        nickName:'',
        myselfDes:''
    },
    //保存按钮
    clickSave(){
        let profileUrl = this.data.profileUrl;
        if (!profileUrl) {
            Toast("请上传头像！");
            return;
        }
        let nickName = this.data.nickName;
        if (!nickName) {
            Toast("请填写昵称");
            return;
        }
        let myselfDes = this.data.myselfDes;
        if (!myselfDes) {
            Toast("请填写自我介绍");
            return;
        }
        let that = this;
        if (profileUrl != _preEnterpriseLogo) {
            //上传图片至OSS
            wx.showLoading({
                title: '图片保存中...',
                mask: true
            });
            let dir = 'xianjian_wx_image';
            // console.log(imgPath);
            that.uploadSyncImg(profileUrl, dir).then(res => {
                if (res.statusCode == 200) {
                    that.saveBaseResume({
                        profileUrl: res.imgUrl,
                        nickName: nickName,
                        myselfDes: myselfDes
                    });
                }
                console.log("upload success：", res);
                wx.hideLoading();
            }, err => {
                wx.hideLoading();
                console.log("err:", err);
                Toast("图片上传失败！")
            });
        } else {
            that.saveBaseResume({
                profileUrl: profileUrl,
                nickName: nickName,
                myselfDes: myselfDes
            });
        }

    },
    //提交信息
    saveBaseResume(data){
        wx.showLoading({
            title: '正在提交...',
            mask: true
        });
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.saveBaseResume,
            data: {
                UserInfoId: app.userInfo.Id,
                Profile: data.profileUrl,
                Name: data.nickName,
                SelfIntroduction: data.myselfDes
            },
            header: { 'Authorization': "Bearer " + app.globalData.token},
            method: 'POST',
            success: function(res) {
                console.log("saveBaseResume success:", res);
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    Toast(res.data.Message);
                    setTimeout(function(){
                        wx.navigateBack({
                            delta: 1,
                        });
                    },600)
                }
            },
            fail: function (res) { console.log("saveBaseResume fail" , res)},
            complete: function(res) {wx.hideLoading();},
        })
    },
    //选择头像
    chooseProfile(){
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                that.setData({
                    profileUrl: res.tempFilePaths[0]
                });
            }
        })
    },
    //昵称输入
    inputNick(e){
        this.setData({
            nickName: e.detail.value
        });
    },
    inputIntroduce(e){
        this.setData({
            myselfDes: e.detail.value
        });
    },
    //上传文件
    uploadSyncImg(path, dir) {
        let promis = new Promise(function (resolve, reject) {
            uploadFile({
                filePath: path,
                dir: dir,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    reject(res);
                }
            });
        });
        return promis;
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let resumeData = app.globalData.resumeInfo
        this.setData({
            profileUrl: resumeData.Profile,
            nickName: resumeData.Name,
            myselfDes: resumeData.SelfIntroduction
        });
        _preEnterpriseLogo = resumeData.Profile;
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})